/**
 * $Id: SGIPReportMessage.java,v 1.1 2006/09/07 02:08:40 martin Exp $
 *
 * Copyright 2005 Skyinfo, Inc. All Rights Reserved.
 */
package com.haoxi.gateway.sgip12msg;

import com.haoxi.gateway.util.TypeConvert;

public class SGIPReportMessage extends SGIPMessage {

    public SGIPReportMessage(byte buf[]) throws IllegalArgumentException {
        super.buf = new byte[56];
        if (buf.length != 56) {
            throw new IllegalArgumentException(
                    "The message that is sent by SMC is wrong");
        } else {
            System.arraycopy(buf, 0, super.buf, 0, 56);
            decodeSeq();
        }
    }

    public byte[] getSubmitSequenceNumber() {
        byte temp[] = new byte[12];
        System.arraycopy(buf, 12, temp, 0, 12);
        return temp;
    }

    public String getSubmitSequenceNumberStr() {
        byte[] temp = getSubmitSequenceNumber();
        int tmp = TypeConvert.byte2int(temp, 0);
        long src_node_Id_tmp = ~(tmp ^ 0xffffffffl);
        if (src_node_Id_tmp < 0) {
            src_node_Id_tmp = tmp;
        }
        int time_Stamp_tmp = TypeConvert.byte2int(temp, 4);
        int sequence_Id_tmp = TypeConvert.byte2int(temp, 8);
        return "" + src_node_Id_tmp + time_Stamp_tmp + sequence_Id_tmp;
    }

    public int getReportType() {
        int tmpId = buf[24];
        return tmpId;
    }

    public String getUserNumber() {
        byte tmpId[] = new byte[21];
        System.arraycopy(buf, 25, tmpId, 0, 21);
        String tmpStr = (new String(tmpId)).trim();
        if (tmpStr.indexOf(0) >= 0)
            return tmpStr.substring(0, tmpStr.indexOf(0));
        else
            return tmpStr;
    }

    public int getState() {
        int tmpId = buf[46];
        return tmpId;
    }

    public int getErrorCode() {
        int tmpId = buf[47];
        return tmpId;
    }

    public String getReserve() {
        byte tmpReserve[] = new byte[8];
        System.arraycopy(buf, 48, tmpReserve, 0, 8);
        return (new String(tmpReserve)).trim();
    }

    public String toString() {
        String tmpStr = new StringBuffer("SGIP_REPORT: ")
                .append("Src_node_Id=").append(getSrcNodeId()).append(
                        ",Time_Stamp=").append(getTimeStamp()).append(
                        ",Sequence_Id=").append(getSequenceId()).append(
                        ",SubmitSequenceNumber=").append(
                        getSubmitSequenceNumberStr()).append(",ReportType=")
                .append(getReportType()).append(",UserNumber=").append(
                        getUserNumber()).append(",State=").append(getState())
                .append(",ErrorCode=").append(getErrorCode()).append(
                        ",Reserve=").append(getReserve()).toString();
        return tmpStr;
    }

    public int getCommandId() {
        return SGIPCommon.SGIP_REPORT;
    }
}
